package com.jcl.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";

    public static final String DATE_FORMAT_YMDHMS_CH = "yyyy年MM月dd HH:mm:ss";
    public static final String DATE_FORMAT_YMDHM_CH = "yyyy年MM月dd HH:mm";
    public static final String DATE_FORMAT_YMD_CH = "yyyy年MM月dd";

    public static List<String> dateSection(String startDate, String endDate, Boolean sortAsc) {
        DateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD);
        Date sDate = null;
        Date eDate = null;
        try {
            sDate = format.parse(startDate);
            eDate = format.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateSection(sDate, eDate, sortAsc);
    }

    public static List<String> dateSection(Date sDate, Date eDate, Boolean sortAsc) {
        DateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD);
        long start = sDate.getTime();
        long end = eDate.getTime();
        if (start > end) {
            return null;
        }
        sortAsc = null == sortAsc ? true: sortAsc;
        List<String> res = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        if(sortAsc){
            calendar.setTime(sDate);
            while (start <= end) {
                res.add(format.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                start = calendar.getTimeInMillis();
            }
        }else{
            calendar.setTime(eDate);
            while (end >= start) {
                res.add(format.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                end = calendar.getTimeInMillis();
            }
        }
        return res;
    }

    public static String format(Date date, String pattern) {
        if (null == date) {
            return null;
        }
        if (null == pattern) {
            pattern = DATE_FORMAT_YMDHMS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String format(Date date) {
        return format(date, null);
    }


    public static Date charToDate(String dateStr, String format) throws ParseException {

        if (null == dateStr || null == format) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    public static Long getDateTime(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, days);
        return c.getTime().getTime();
    }

    public static Integer formatAge(String day) {

        if (null == day) {
            return null;
        }

        String format = null;
        if (day.length() > 19) {
            day = day.substring(0, 19);
            format = DATE_FORMAT_YMDHMS;
        } else if (day.length() > 10 && day.length() < 19) {
            day = day.substring(0, 10);
            format = DATE_FORMAT_YMD;
        } else if (day.length() == 10) {
            format = DATE_FORMAT_YMD;
        }
        Date birthDay = null;
        try {
            birthDay = charToDate(day, format);

            return formatAge(birthDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Integer formatAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return null;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;

    }

    public static void main(String[] args) {
        System.out.println(dateSection("2018-12-01", "2018-12-25", null));
    }
}
